//    Dhammapada PSP
//    Copyright (C) 2007 Sakya
//    sakya_tg@yahoo.it
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#include <pspkernel.h>
#include <pspsdk.h>
#include <stdio.h>
#include <oslib/oslib.h>

#include "options.h"
#include "dhammapada.h"
#include "languages.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Globals:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int optionsRetValue = 0;
static int exitFlag = 0;
struct settings *lUserSettings;
int currentLangIndex = 0;
struct menuElements tMenu;
char buffer[262] = "";

void buildOptionsMenu();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Back to main menu:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int optionCancel(){
    optionsRetValue = MODE_MAINMENU;
    exitFlag = 1;
    SETTINGS_load(lUserSettings->fileName);
    sprintf(buffer, "%s/data/%s/lang.txt", lUserSettings->ebootPath, lUserSettings->lang);
    langLoad(buffer);
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Confirm options:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int optionOK(){
    SETTINGS_save(lUserSettings);
    loadCategoriesIndexOriginal();
    loadCategoriesIndex(lUserSettings->lang);
    optionsRetValue = MODE_MAINMENU;
    exitFlag = 1;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Change current language:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int changeLanguage(){
    if (++currentLangIndex > languagesCount - 1)
        currentLangIndex = 0;
    strcpy(lUserSettings->lang, languagesList[currentLangIndex]);
    sprintf(buffer, "%s/data/%s/lang.txt", lUserSettings->ebootPath, lUserSettings->lang);
    langLoad(buffer);
    buildOptionsMenu();
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Build options menu:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void buildOptionsMenu(){
    struct menuElement tMenuEl;
    tMenu.yPos = 45;
    tMenu.xPos = 65;
    tMenu.width = 350;
    tMenu.height = 200;
    sprintf(buffer, "%s/images/menubkg.png", lUserSettings->ebootPath);
    tMenu.background = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!tMenu.background)
        errorLoadImage(buffer);
    tMenu.interline = 3;
    tMenu.first = 0;
    tMenu.selected = 0;
    tMenu.numberOfElements = 3;
    tMenu.maxNumberVisible = 3;
    tMenu.cancelFunction = optionCancel;
    strcpy(tMenuEl.name, "LANGUAGE");
    snprintf(tMenuEl.text, sizeof(tMenuEl.text), "%s: %s", langGetString(tMenuEl.name), lUserSettings->lang);
    strcpy(tMenuEl.helpText, langGetString("LANGUAGE_HELP"));
    tMenuEl.triggerFunction = changeLanguage;
    tMenu.elements[0] = tMenuEl;
    strcpy(tMenuEl.name, "OPT_OK");
    strcpy(tMenuEl.text, langGetString(tMenuEl.name));
    strcpy(tMenuEl.helpText, langGetString("OPT_OK_HELP"));
    tMenuEl.triggerFunction = optionOK;
    tMenu.elements[1] = tMenuEl;
    strcpy(tMenuEl.name, "OPT_CANCEL");
    strcpy(tMenuEl.text, langGetString(tMenuEl.name));
    strcpy(tMenuEl.helpText, langGetString("OPT_CANCEL_HELP"));
    tMenuEl.triggerFunction = optionCancel;
    tMenu.elements[2] = tMenuEl;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Options menu:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int optionsMenu(struct settings *userSettings){
    char buffer[262];
    int i = 0;

    optionsRetValue = -1;
    lUserSettings = userSettings;
    
    //Load images:
    sprintf(buffer, "%s/images/bkg_default.png", userSettings->ebootPath);
    OSL_IMAGE *bkg = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!bkg)
        errorLoadImage(buffer);

    sprintf(buffer, "%s/images/toolbar.png", userSettings->ebootPath);
    OSL_IMAGE *toolbar = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!toolbar)
        errorLoadImage(buffer);

    //Get current lang index:
    for (i=0; i<languagesCount; i++){
        if (!strcmp(languagesList[i], lUserSettings->lang)){
            currentLangIndex = i;
            break;
        }
    }
    
    buildOptionsMenu();
    exitFlag = 0;
    while(!osl_quit && !exitFlag){
        oslStartDrawing();
        oslDrawImageXY(bkg, 0, 0);
        oslDrawImageXY(toolbar, 0, 272 - toolbar->sizeY);
        drawMenu(&tMenu);

        oslReadKeys();
        processMenuKeys(&tMenu);
    	oslEndDrawing();
    	oslSyncFrame();
    	oslAudioVSync();
    }
    //unLoad images:
    clearMenu(&tMenu);
    oslDeleteImage(bkg);
    return optionsRetValue;
}
